Scaled field


In [1]:
from konfoo import Index, Byteorder, Scaled, Scaled8, Scaled16, Scaled24, Scaled32, Scaled64

Item

Item type of the field class.


In [2]:
Scaled.item_type


Out[2]:
ItemClass.Scaled = 49

Checks if the field class is a bit field.


In [3]:
Scaled.is_bit()


Out[3]:
False

Checks if the field class is a boolean field.


In [4]:
Scaled.is_bool()


Out[4]:
False

Checks if the field class is a decimal number field.


In [5]:
Scaled.is_decimal()


Out[5]:
True

Checks if the field class is a floating point number field.


In [6]:
Scaled.is_float()


Out[6]:
False

Checks if the field class is a pointer field.


In [7]:
Scaled.is_pointer()


Out[7]:
False

Checks if the field class is a stream field.


In [8]:
Scaled.is_stream()


Out[8]:
False

Checks if the field class is a string field.


In [9]:
Scaled.is_string()


Out[9]:
False

Field


In [10]:
scaled = Scaled(scale=100.0, bit_size=32, align_to=None, byte_order='auto')

In [11]:
scaled = Scaled(100.0, 32)

Field view


In [12]:
scaled


Out[12]:
Scaled(index=Index(byte=0, bit=0, address=0, base_address=0, update=False), alignment=Alignment(byte_size=4, bit_offset=0), bit_size=32, value=0.0)

In [13]:
str(scaled)


Out[13]:
'Scaled32(Index(byte=0, bit=0, address=0, base_address=0, update=False), Alignment(byte_size=4, bit_offset=0), 32, 0.0)'

In [14]:
repr(scaled)


Out[14]:
'Scaled(index=Index(byte=0, bit=0, address=0, base_address=0, update=False), alignment=Alignment(byte_size=4, bit_offset=0), bit_size=32, value=0.0)'

Field name


In [15]:
scaled.name


Out[15]:
'Scaled32'

Field index


In [16]:
scaled.index


Out[16]:
Index(byte=0, bit=0, address=0, base_address=0, update=False)

Byte index of the field within the byte stream.


In [17]:
scaled.index.byte


Out[17]:
0

Bit offset relative to the byte index of the field within the byte stream.


In [18]:
scaled.index.bit


Out[18]:
0

Absolute address of the field within the data source.


In [19]:
scaled.index.address


Out[19]:
0

Base address of the byte stream within the data source.


In [20]:
scaled.index.base_address


Out[20]:
0

Indexes the field and returns the index after the field.


In [21]:
scaled.index_field(index=Index())


Out[21]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

Field alignment


In [22]:
scaled.alignment


Out[22]:
Alignment(byte_size=4, bit_offset=0)

Byte size of the field group which the field is aligned to.


In [23]:
scaled.alignment.byte_size


Out[23]:
4

Bit offset of the field within its aligned field group.


In [24]:
scaled.alignment.bit_offset


Out[24]:
0

Field size


In [25]:
scaled.bit_size


Out[25]:
32

Field byte order


In [26]:
scaled.byte_order


Out[26]:
Byteorder.auto = 'auto'

In [27]:
scaled.byte_order.value


Out[27]:
'auto'

In [28]:
scaled.byte_order.name


Out[28]:
'auto'

In [29]:
scaled.byte_order = 'auto'

In [30]:
scaled.byte_order = Byteorder.auto

Field scaling

Scaling factor of the field value.


In [31]:
scaled.scale


Out[31]:
100.0

Base value for the scale factor of the field value.


In [32]:
scaled.scaling_base()


Out[32]:
1073741824.0

In [33]:
hex(int(scaled.scaling_base()))


Out[33]:
'0x40000000'

Field value

Checks if the decimal field is signed or unsigned.


In [34]:
scaled.signed


Out[34]:
True

Maximal decimal field value.


In [35]:
scaled.max()


Out[35]:
2147483647

Minimal decimal field value.


In [36]:
scaled.min()


Out[36]:
-2147483648

Returns the scaled field value as an floating point number.


In [37]:
scaled.value


Out[37]:
0.0

Returns the decimal field value aligned to its field group as a number of bytes.


In [38]:
bytes(scaled)


Out[38]:
b'\x00\x00\x00\x00'

In [39]:
bytes(scaled).hex()


Out[39]:
'00000000'

Returns the decimal field value as an integer number.


In [40]:
int(scaled)


Out[40]:
0

Returns the decimal field value as an floating point number.


In [41]:
float(scaled)


Out[41]:
0.0

Returns the decimal field value as a lowercase hexadecimal string prefixed with 0x.


In [42]:
hex(scaled)


Out[42]:
'0x0'

Returns the decimal field value as a binary string prefixed with 0b.


In [43]:
bin(scaled)


Out[43]:
'0b0'

Returns the decimal field value as an octal string prefixed with 0o.


In [44]:
oct(scaled)


Out[44]:
'0o0'

Returns the decimal field value as a boolean value.


In [45]:
bool(scaled)


Out[45]:
False

Returns the decimal field value as a signed integer number.


In [46]:
scaled.as_signed()


Out[46]:
0

Returns the decimal field value as an unsigned integer number.


In [47]:
scaled.as_unsigned()


Out[47]:
0

Field metadata

Returns the meatadata of the field as an ordered dictionary.


In [48]:
scaled.describe()


Out[48]:
OrderedDict([('address', 0),
             ('alignment', [4, 0]),
             ('class', 'Scaled32'),
             ('index', [0, 0]),
             ('max', 2147483647),
             ('min', -2147483648),
             ('name', 'Scaled32'),
             ('order', 'auto'),
             ('scale', 100.0),
             ('signed', True),
             ('size', 32),
             ('type', 'Field'),
             ('value', 0.0)])

Deserialize


In [49]:
scaled.deserialize(bytes.fromhex('01000000'), byte_order='little')


Out[49]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

In [50]:
scaled.value


Out[50]:
9.313225746154785e-08

In [51]:
bytes(scaled)


Out[51]:
b'\x01\x00\x00\x00'

In [52]:
bytes(scaled).hex()


Out[52]:
'01000000'

In [53]:
int(scaled)


Out[53]:
1

In [54]:
float(scaled)


Out[54]:
9.313225746154785e-08

In [55]:
hex(scaled)


Out[55]:
'0x1'

In [56]:
bin(scaled)


Out[56]:
'0b1'

In [57]:
oct(scaled)


Out[57]:
'0o1'

In [58]:
bool(scaled)


Out[58]:
True

Serialize


In [59]:
buffer = bytearray()

In [60]:
scaled.value = 1

In [61]:
scaled.value = 1.0

In [62]:
scaled.value = 0x1

In [63]:
scaled.value = 0b1

In [64]:
scaled.value = 0o1

In [65]:
scaled.value = True

In [66]:
scaled.value = 1.0

In [67]:
scaled.serialize(buffer, byte_order='little')


Out[67]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

In [68]:
buffer.hex()


Out[68]:
'0ad7a300'

In [69]:
bytes(scaled).hex()


Out[69]:
'0ad7a300'

In [ ]: